// pages/d-center/dealerOrder.js
var app = getApp()
Page({
  data: {
    statusType: ["待处理", "已处理","已取消"],
    currentType: 0,
    tabClass: ["", "", "", "", ""],
    bodyHeight:null,
    index:1,
    isFinish:false,
    orderList:[],
  },
  logisticsTap(e) {
    var orderId = e.currentTarget.dataset.id;
    if( orderId ){
      wx.navigateTo({
        url: '/pages/logistics/logistics?id=' + orderId,
      })
    }else{
      wx.showToast({
        title: '等待物流公司发货中',
        icon: 'none'
      })
      return
    }
  },
  statusTap: function (e) {
    console.log(e);
    console.log(e.hasOwnProperty("currentTarget"))
    if(e&&e.hasOwnProperty("currentTarget")){
        this.setData({
          currentType: e.currentTarget.dataset.index
        });
    }else{
      this.setData({
        currentType: e.currentType
      });
    }   
    this.setData({
      orderList:[],
      isFinish:false,
      index:1,
    })
    this.getData();
    // this.onShow();
  },
  orderDetail: function (e) {
    var orderId = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: "/pages/dealerOrder-detail/dealerOrder-detail?id=" + orderId + '&share=1'
    })
  },
  cancelOrderTap: function (e) {
    var that = this;
    var orderId = e.currentTarget.dataset.id;
    var custId=e.currentTarget.dataset.custid;
    wx.showModal({
      title: '确定要取消该订单吗？',
      content: '',
      success: function (res) {
        if (res.confirm) {
          wx.showLoading(           {
            title: '加载中...',
            mask: true});
          app.postForm("dealerorder/test_cancel_dealer_order",{
            CustId: custId,
            DealerOrderId: orderId
          },res=>{
            wx.hideLoading();
            that.statusTap({currentType:2});  
            if( res.code == -1 ){
              wx.showToast({
                title: res.msg,
                icon: 'none'
              })
            }else{
              wx.showToast({
                title: '取消订单成功'
              })
            }
          })
        }
      }
    })
  },
  toPayTap: function (e) {
    wx.showLoading(           {
      title: '加载中...',
      mask: true});
    var that = this;
    var orderId = e.currentTarget.dataset.id;
    var money = e.currentTarget.dataset.money;

    let payData = {
      OrderIds: orderId,
      OpenId: app.globalData.openid,
      Type: 'JSAPI'
    }
    app.postForm('order/pay_order_by_orderids', payData, rep => {
      // console.log(rep)
      wx.hideLoading();
    })
    
  },  
  onLoad: function (e) {
    var that = this;
    var currentType;
    wx.hideShareMenu()
    if (app.globalData.iphone == true) { that.setData({ iphone: 'iphone' }) } 
    that.statusTap({
      currentType: 0
    });    
  },
  onReady: function () {
    // 生命周期函数--监听页面初次渲染完成

  },
  getOrderStatistics: function () {
    var that = this;    
  },
	toConfirmTap:function(e){
	  let that = this;
	  let orderId = e.currentTarget.dataset.id;
	  let formId = e.detail.formId;
	  wx.showModal({
	      title: '确认您已收到商品？',
	      content: '',
	      success: function(res) {
	        if (res.confirm) {
	          wx.showLoading(           {
              title: '加载中...',
              mask: true});
	          app.postForm("dealerorder/receive_dealer_order",{
              CustId: app.globalData.uid,
              DealerOrderId: orderId
            },res=>{
              wx.hideLoading({
                success: (res) => {},
              })
              if(res&&res.code=='0'){
                that.statusTap({currentType:2});   
              }
            })
	        }
	      }
	  })
  },
  sendOrderTap(e){
    let DealerOrderId=e.currentTarget.dataset.id;
    let that=this;
    wx.showModal({
      title: '确定要发货吗？',
      content: '',
      success: function (res) {
        if (res.confirm) {
          wx.showLoading({
            title: '处理中',
            mask:true,
          })
          app.postForm('dealerorder/test_send_order', {
            DealerOrderId
          }, res => {
            if( res && res.code == "0" ){
              wx.hideLoading()
              wx.showToast({
                title: '已确认发货，等待物流处理中'
              })
              that.statusTap({
                currentType: that.data.currentType
              })
            }else{
              wx.hideLoading()
              wx.showToast({
                title: res.msg,
                icon: 'none'
              })
            }
          })
        }
      }
    })
  },
  getData:function(status,index){
    let _this=this;
    if(this.data.isFinish){
      return
    }
    wx.showLoading({
      title: '加载中...',
    })
    let sta=0;
    switch(this.data.currentType){
      case 0: sta=1
      break;
      case 1:sta=2
      break;
      case 2:sta=13
      break;
      case 3:sta=13
      break;
      default:
        break;
    }    
    app.get(`dealerorder/test_get_dealer_orders?DealerCustId=${app.globalData.uid}&Submitter=m&Status=${sta}&page=${this.data.index}&size=20`,(res)=>{
      wx.hideLoading();
      if(res&&res.code=="0"){
        if(res.result.length>0){
          _this.setData({
            orderList:_this.data.orderList.concat(res.result),
            index:this.data.index+1
          });
        }else{          
          _this.setData({
            isFinish:true,
          })
        } 
      }
    })
  },  
  onShow: function (e) {
    // 获取订单列表
    // wx.showLoading();
    var that = this;
  },
  onHide: function () {
    // 生命周期函数--监听页面隐藏

  },
  onUnload: function () {
    // 生命周期函数--监听页面卸载

  },
  onPullDownRefresh: function () {
    // 页面相关事件处理函数--监听用户下拉动作

  },
  onReachBottom: function () {
    // 页面上拉触底事件的处理函数

  }
})